package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.Product;
import com.spzx.product.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-02-19
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;


    /**
     * 分页查询商品SPU列表
     *
     * @return
     */
    @Operation(summary = "分页查询商品SPU列表")
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        List<Product> list = productService.selectProductList();
        return getDataTable(list);
    }


    @PostMapping
    @Operation(summary = "保存商品（商品信息、商品详情、商品SKU列表以及SKU库存）")
    public AjaxResult saveProduct(@RequestBody Product product) {
        productService.saveProduct(product);
        return success();
    }


    @Operation(summary = "商品上下架")
    @GetMapping("/updateStatus/{productId}/{status}")
    public AjaxResult updateStatus(@PathVariable Long productId, @PathVariable Integer status) {
        if (status != 1 && status != -1) {
            return error("状态有误！");
        }
        productService.updateStatus(productId, status);
        return success();
    }


    @Operation(summary = "批量删除SPU相关信息")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        productService.removeProduct(ids);
        return success();
    }

}
